<?php

namespace catchAdmin\model;

use catcher\Tree;
use think\facade\Cache;

class Area extends ModelApp
{


    public function indexTree()
    {
        if (!empty(Cache::get('city_tree'))) {
            return Cache::get('city_tree');
        }

        $list = Tree::done($this
            ->select()
            ->toArray());

        $list_2 = [];
        foreach ($list as $item) {
            $tmp = [
                'value' => $item['code'],
                'label' => $item['name'],
            ];
            foreach ($item['children'] as $child) {
                $tmp_2 = [
                    'value' => $child['code'],
                    'label' => $child['name'],
                ];
                foreach ($child['children'] as $child_2) {
                    $tmp_2['children'][] = [
                        'value' => $child_2['code'],
                        'label' => $child_2['name'],
                    ];
                }
                $tmp['children'][] = $tmp_2;
            }

            $list_2[] = $tmp;
        }

        Cache::set('city_tree', $list_2);

        return $list_2;
    }

}
